<!-- 贸易方式 -->
<template>
  <div class="page-container">
    <div class="in-container">
      <AppSearchForm @submit="getList" @reset="reset">
        <el-form-item>
          <el-input
            v-model="query.KeyWords"
            clearable
            placeholder="请输入代码、简称或全称"
            style="width: 300px"
          />
        </el-form-item>
      </AppSearchForm>
      <div class="btn-area">
        <el-button @click="openDialog" type="primary"> 新增 </el-button>
        <el-button @click="exportFile" type="primary"> EXCEL导出 </el-button>
        <el-button @click="deleteSelectData" type="danger"> 删除 </el-button>
      </div>
      <AppPageTable
        stripe
        :data="tableData.data"
        @change="getList()"
        :autostyle="'height: calc(100% - 80px);'"
        :option="tableData.option"
      >
        <el-table-column
          show-overflow-tooltip
          type="selection"
          width="55"
          fixed="left"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="code"
          width="100"
          label="代码"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="abbreviation"
          width="150"
          label="简称"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="fullTitle"
          width="120"
          label="全称"
        />

        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button @click.stop="update(scope.row)" type="primary"
              >编辑</el-button
            >
            <el-popconfirm
              title="确定删除本条数据吗？"
              @confirm="deleteData(scope.row)"
            >
              <template #reference>
                <el-button type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </AppPageTable>
      <el-dialog
        :title="dialogData.title"
        v-model="dialogData.show"
        width="500px"
      >
        <el-form
          :model="formData"
          :rules="formRules"
          label-width="80px"
          ref="form"
        >
          <el-form-item prop="code" label="代码">
            <el-input v-model="formData.code"></el-input>
          </el-form-item>
          <el-form-item prop="abbreviation" label="简称">
            <el-input v-model="formData.abbreviation"></el-input>
          </el-form-item>
          <el-form-item prop="fullTitle" label="全称">
            <el-input v-model="formData.fullTitle"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="cancel" link>取消</el-button>
            <el-button type="primary" @click="submit"> 确认 </el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { defineComponent } from "vue";
import AppSearchForm from "@/components/AppSearchForm.vue";
import AppPageTable, { useTableOption } from "@/components/AppPageTable2";
import AppTitleBox from "@/components/AppTitleBox.vue";
import { fileDownload } from "@/public/publicUtils";
import { ElMessage, ElMessageBox } from "element-plus";

export default defineComponent({
  components: {
    AppSearchForm,
    AppPageTable,
    AppTitleBox,
  },
  data() {
    return {
      query: {
        KeyWords: "",
      },
      loading: false,
      tableData: {
        data: [],
        option: useTableOption(),
      },
      dialogData: {
        show: false,
        title: "",
        btnLoading: false,
      },
      formData: {
        code: "",
        abbreviation: "",
        fullTitle: "",
      },
      topNationName: "",
      topNationId: "",
      formRules: {
        code: [{ required: true, message: "请输入代码", trigger: "blur" }],
        abbreviation: [{ required: true, message: "请输入简称", trigger: "blur" }],
        fullTitle: [
          { required: true, message: "请输入全称", trigger: "blur" },
        ],
      },
      nationName: "", //国家
      options: [],
    };
  },
  methods: {
    getList() {
      this.tableData.option.loading = true;
        this.$http.get("/api/base/tradeType/query", {
          PageIndex: this.tableData.option.page,
          PageSize: this.tableData.option.pageSize,
          KeyWords: this.query.KeyWords
        }).then((res) => {
            this.tableData.data = res.content;
            this.tableData.option.total = res.totalElements;
        })
        .finally(() => {
            this.tableData.option.loading = false;
        });
    },
    // 清空输入框
    clearTopSelect() {
      this.topNationName = "";
      this.query.KeyWords = "";
    },
    reset() {
      this.tableData.option.page = 1;
      this.tableData.option.pageSize = 15;
      this.query.KeyWords = "";
      this.topNationName = "";
      this.getList();
    },
    update(data) {
      console.log("data", data);
      this.nationName = data.nationName;
      this.formData = JSON.parse(JSON.stringify(data));
      this.dialogData.title = "编辑城市";
      this.dialogData.show = true;
    },
    deleteData(data) {
      console.log("---data---", data);
      this.$http
        .post("/api/base/tradeType/delete", {
          idArray: [data.id],
        })
        .then((res) => {
          if (res.status) {
            this.$message.success(res.message);
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        });
    },
    async submit() {
      this.dialogData.btnLoading = true;
      await this.$refs.form.validate();
      if (this.formData.id) {
        this.$http.post("/api/base/tradeType/edit", this.formData).then((res) => {
          if (res.status) {
            this.$message.success(res.message);
            this.getList();
            this.dialogData.show = false;
          } else {
            this.$message.error(res.message);
          }
          this.dialogData.btnLoading = false;
        });
      } else {
        this.$http.post("/api/base/tradeType/create", this.formData).then((res) => {
          if (res.status) {
            this.$message.success(res.message);
            this.getList();
            this.dialogData.show = false;
          } else {
            this.$message.error(res.message);
          }
          this.dialogData.btnLoading = false;
        });
      }
    },
    cancel() {
      this.dialogData.show = false;
    },
    openDialog() {
      this.nationName = "";
      this.dialogData.title = "新增城市";
      this.dialogData.show = true;
    },
    exportFile() {
      fileDownload("/api/base/tradeType/download", this.query, "贸易方式")
        .then(() => {})
        .catch((err) => {
          ElMessage.warning("文件导出失败");
        });
    },
    // 查询所有数据
    async getNation() {
      let res = await this.$http.get("/api/base/tradeType/queryall");
      this.options = res.content;
    },
    // 城市发生改变
    changeNation(e) {
      this.formData.nationName = e;
      this.formData.nationId = this.options.filter(
        (item) => item.nationName == e
      )[0].id;
    },
    changeTopNation(e) {
      this.topNationName = e;
      this.topNationId = this.options.filter(
        (item) => item.nationName == e
      )[0].id;
    },
    /**
     * 多项删除业务
    */
    deleteSelectData() {
      let deleteSelectArray = [];
      this.tableData.option.selected.forEach((t) =>
        deleteSelectArray.push(t.id)
      );
      ElMessageBox.confirm("确定删除所选城市信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
          this.$http.post("/api/base/tradeType/delete", {
              idArray: deleteSelectArray,
            }).then((res) => {
              if (res.status) {
                this.$message.success(res.message);
                this.getList();
              } else {
                this.$message.error(res.message);
              }
            });
        })
        .catch(() => {
          this.$message.info("取消删除");
        });
    },
  },
  watch: {
    "dialogData.show"(newVal) {
      if (!newVal) {
        this.formData = {
          nationCode: "",
          nationName: "",
          nationEnName: "",
          customCode: "",
        };
        this.$nextTick(() => {
          this.$refs.form.clearValidate();
        });
      }
    },
  },
  created() {
    this.getList();
  },
});
</script>

<style lang="scss" scoped>
.btn-area {
  margin-bottom: 8px;
}
.form .el-select--small {
  width: 100px !important;
}
::v-deep .page_component .el-select--small {
    width: 100px !important;
}
#select_nation {
  margin-left: 20px;
}
</style>
